package com.dt.global.web.dao.Newscentrality.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.springframework.stereotype.Repository;

import com.dt.components.exhibernate.impl.HibernateDaoImpl;
import com.dt.global.entitys.bo.TWebCoreArticle;
import com.dt.global.entitys.bo.TWebCoreChannel;
import com.dt.global.web.dao.Newscentrality.NewscentralityDao;

/**
 * 首页新闻查询的实现类
 * @author MWG
 *
 */
@SuppressWarnings("rawtypes")
@Repository
public class NewscentralityImpl extends HibernateDaoImpl implements NewscentralityDao{
	private static final Logger LOG = Logger.getLogger(NewscentralityImpl.class);
	public List<TWebCoreChannel> getTWebCoreChannelPid(String pid)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
		    String sql="from TWebCoreChannel where pid =? and isDisplay='Y'  order by priority";
		    param.add(pid);
			@SuppressWarnings("unchecked")
			List<TWebCoreChannel> list = this.find(sql,param);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}

	public TWebCoreChannel getTWebCoreChannelId(String ChannelId)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
			TWebCoreChannel channel =null;
		    String sql="from TWebCoreChannel where channelId=? ";
		    param.add(ChannelId);
			@SuppressWarnings("unchecked")
			List<TWebCoreChannel> list = this.find(sql,param);
			if(list !=null&&list.size()>0){
				channel=list.get(0);
			}
			return channel;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}

	public List<TWebCoreArticle> getTWebCoreArticle(String ChannelId)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
			 //得到当期系统时间
			   SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
			   String lastModify=df.format(new Date());
			String sql = "from TWebCoreArticle where  ('"+lastModify+"'<=endDate or endDate is null or endDate='') and channelId = ? and isDisabled='Y' order by releaseSysDate desc";
			param.add(ChannelId);
			List<TWebCoreArticle> list = this.find(sql,param);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}

	public List<TWebCoreChannel> getTWebCoreChannelNid(String nid)
			throws HibernateException {
		try {
			List<String> param = new ArrayList<String>();
		    String sql="from TWebCoreChannel where nid =? order by priority";
		    param.add(nid);
			@SuppressWarnings("unchecked")
			List<TWebCoreChannel> list = this.find(sql,param);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<String> getTWebCoreChannelGroupByNid(String pid)
			throws HibernateException {
		try {
			List<String> list=null;
			List<String> param = new ArrayList<String>();
			if(pid.equals("00")){
				   String sql="select nid from TWebCoreChannel where (nid is not null or nid !='') and (priority is not null or priority!='') and pid=? and isDisplay='Y' order by priority";
				   param.add(pid);
				   list = this.find(sql,param,0,8);
			}else{
				String sql="select nid from TWebCoreChannel where (nid is not null or nid !='') and len(nid)='4' and pid=? and isDisplay='Y' order by priority";
				 param.add(pid);
			    list = this.find(sql,param);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			LOG.error(e);
			return null;
		}
	}

}
